package main;

import java.io.*;
import java.util.ArrayList;

/**
 * @author Xu,
 * @data 2020/11/29，
 * @time 14:11
 */
public class Bayesian {

    private ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

    /**
     * 导入数据
     * @param path
     * @return
     */
    public ArrayList<ArrayList<String>> readData(String path){
        ArrayList<String> d = null;
        ArrayList<ArrayList<String>> t = new ArrayList<ArrayList<String>>();

        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(path)));
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while((str = br.readLine()) != null){
                d = new ArrayList<String>();
                String[] str1 = str.split(",");
                for(int i = 0; i < str1.length ; i++) {
                    d.add(str1[i]);
                }
                t.add(d);
                str = br.readLine();
            }
            data = t;
            br.close();
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件内容出错！");
        }
        return t;
    }

    public void calculate_Bayesian(String purchase, String maintain, String car_door, String person_num, String size, String security){
        //获取P(unacc|acc|good|vgood)的分子————————————P(x|y)的分母
        int unacc = getNums("unacc");
        int acc = getNums("acc");
        int good = getNums("good");
        int vgood = getNums("vgood");
        //获取P(x|y)的分子,0代表购买价格，1代表维护费用，2代表车门数，3代表可载人数，4代表车身大小，5代表安全程度
        int purchase_unacc = getNums(0,purchase,"unacc");
        int maintain_unacc = getNums(1, maintain, "unacc");
        int car_door_unacc = getNums(2, car_door, "unacc");
        int person_num_unacc = getNums(3, person_num, "unacc");
        int size_unacc = getNums(4, size, "unacc");
        int security_unacc = getNums(5, security, "unacc");
        int purchase_acc = getNums(0,purchase,"acc");
        int maintain_acc = getNums(1, maintain, "acc");
        int car_door_acc = getNums(2, car_door, "acc");
        int person_num_acc = getNums(3, person_num, "acc");
        int size_acc = getNums(4, size, "acc");
        int security_acc = getNums(5, security, "acc");
        int purchase_good = getNums(0,purchase,"good");
        int maintain_good = getNums(1, maintain, "good");
        int car_door_good = getNums(2, car_door, "good");
        int person_num_good = getNums(3, person_num, "good");
        int size_good = getNums(4, size, "good");
        int security_good = getNums(5, security, "good");
        int purchase_vgood = getNums(0,purchase,"vgood");
        int maintain_vgood = getNums(1, maintain, "vgood");
        int car_door_vgood = getNums(2, car_door, "vgood");
        int person_num_vgood = getNums(3, person_num, "vgood");
        int size_vgood = getNums(4, size, "vgood");
        int security_vgood = getNums(5, security, "vgood");

        //计算概率,unacc,acc,good,vgood
        double unacc_probability = (unacc/(data.size()*1.0))*(purchase_unacc/(unacc*1.0))
                *(maintain_unacc/(unacc*1.0))*(car_door_unacc/(unacc*1.0))*(person_num_unacc/(unacc*1.0))
                *(size_unacc/(unacc*1.0))*(security_unacc/(unacc*1.0));
        double acc_probability = (acc/(data.size()*1.0))*(purchase_acc/(acc*1.0))
                *(maintain_acc/(acc*1.0))*(car_door_acc/(acc*1.0))*(person_num_acc/(acc*1.0))
                *(size_acc/(acc*1.0))*(security_acc/(acc*1.0));
        double good_probability = (good/(data.size()*1.0))*(purchase_good/(good*1.0))
                *(maintain_good/(good*1.0))*(car_door_good/(good*1.0))*(person_num_good/(good*1.0))
                *(size_good/(good*1.0))*(security_good/(good*1.0));
        double vgood_probability = (vgood/(data.size()*1.0))*(purchase_vgood/(vgood*1.0))
                *(maintain_vgood/(vgood*1.0))*(car_door_vgood/(vgood*1.0))*(person_num_vgood/(vgood*1.0))
                *(size_vgood/(vgood*1.0))*(security_vgood/(vgood*1.0));

        //比较概率得出结论
        //System.out.println(unacc_probability+"\n"+acc_probability+"\n"+good_probability+"\n"+vgood_probability);

        if(unacc_probability > acc_probability
                && unacc_probability > good_probability
                && unacc_probability > vgood_probability){
            System.out.println("<"+purchase+","+maintain+","+car_door+","+person_num+","+size+","+security+">  ---> 不可接受");
        }
        if(acc_probability > unacc_probability
                && acc_probability > good_probability
                && acc_probability > vgood_probability){
            System.out.println("<"+purchase+","+maintain+","+car_door+","+person_num+","+size+","+security+">  ---> 可接受");
        }
        if(good_probability > unacc_probability
                && good_probability > acc_probability
                && good_probability > vgood_probability){
            System.out.println("<"+purchase+","+maintain+","+car_door+","+person_num+","+size+","+security+">  ---> 好");
        }
        if(vgood_probability > unacc_probability
                && vgood_probability > acc_probability
                && vgood_probability > good_probability){
            System.out.println("<"+purchase+","+maintain+","+car_door+","+person_num+","+size+","+security+">  ---> 很好");
        }

    }


    public void show(){
        for(int i=0;i<data.size();i++){
            System.out.println(data.get(i));
        }
    }

    public void here(){
        System.out.println("unacc = " + getNums("unacc"));
        System.out.println("acc = " + getNums("acc"));
        System.out.println("good = " + getNums("good"));
        System.out.println("vgood = " + getNums("vgood"));
        System.out.println("total = " + (getNums("unacc")+getNums("acc")
                +getNums("good")+getNums("vgood")));

    }

    /**
     * 获取P(unacc|acc|good|vgood)的分子，P(x|y)的分母
     * @return
     */
    private int getNums(String str) {
        int num = 0;
        for(int i=0;i<data.size();i++){
            if(data.get(i).get(6).equals(str)){
                num++;
            }
        }
        return num;
    }

    /**
     * 获取P(x|y)的分子
     * @param id
     * @param str1
     * @param str2
     * @return
     */
    private int getNums(int id, String str1, String str2){
        int num = 0;
        for(int i=0;i<data.size();i++){
            if(data.get(i).get(6).equals(str2)){
                if(data.get(i).get(id).equals(str1)){
                    num++;
                }
            }
        }
        return num;
    }

}
